package cn.itcast.weatherapp123.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import cn.itcast.weatherapp123.R;
import cn.itcast.weatherapp123.bean.DayWeatherBean;

public class FutureWeatherAdapter extends RecyclerView.Adapter<FutureWeatherAdapter.WeatherViewHold> {

    private Context mContext;
    private List<DayWeatherBean> mWeatherBeans;

    public FutureWeatherAdapter(Context context,List<DayWeatherBean> weatherBeans){

        mContext=context;
        mWeatherBeans=weatherBeans;

    }

    @NonNull
    @Override
    public WeatherViewHold onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view=LayoutInflater.from(mContext).inflate(R.layout.weather_item_layout,parent,false);
        WeatherViewHold weatherViewHold=new WeatherViewHold(view);
        return weatherViewHold;

    }

    @Override
    public void onBindViewHolder(@NonNull WeatherViewHold holder, int position) {

             DayWeatherBean weatherBean=mWeatherBeans.get(position);

             holder.tvWeather.setText(weatherBean.getWea());
             holder.tvTem.setText(weatherBean.getTem());
             holder.tvTemLowHigh.setText(weatherBean.getTem2()+"-"+weatherBean.getTem1());
             holder.tvWin.setText(weatherBean.getWin()[0]+weatherBean.getWinSpeed());
             holder.tvAir.setText("空气"+weatherBean.getAir()+weatherBean.getAirLevel());
             holder.tvDate.setText("("+weatherBean.getDate()+weatherBean.getWeek()+")");
             holder.ivWeather.setImageResource(getImResOfWeather(weatherBean.getWeaImg()));
    }

    @Override
    public int getItemCount() {

        if (mWeatherBeans==null){
        return 0;
    }
        return (mWeatherBeans==null)?0:mWeatherBeans.size();

}

    class WeatherViewHold extends RecyclerView.ViewHolder{

        private TextView tvWeather, tvTem, tvTemLowHigh, tvWin, tvAir,tvDate;
        private ImageView ivWeather;


        public WeatherViewHold(@NonNull View itemView) {
            super(itemView);

            tvWeather=itemView.findViewById(R.id.tv_mweather);
            tvAir=itemView.findViewById(R.id.tv_air);
            tvTem=itemView.findViewById(R.id.tv_tem);
            tvTemLowHigh=itemView.findViewById(R.id.tv_tem_low_high);
            tvWin=itemView.findViewById(R.id.tv_win);
            ivWeather=itemView.findViewById(R.id.iv_weather);
            tvDate=itemView.findViewById(R.id.tv_date);
        }
    }

    //判断天气返回照片
    public int getImResOfWeather(String weaStr) {

        int result = 0;

        switch (weaStr) {
            case "qing":
                result = R.drawable.duoyun;
                break;
            case "yin":
                result = R.drawable.yintian;
                break;
            case "yu":
                result = R.drawable.yun;
                break;
            case "bingbao":
                result = R.drawable.bingbao;
                break;
            case "wu":
                result = R.drawable.wu;
                break;
            case "shachen":
                result = R.drawable.shachen;
                break;
            case "lei":
                result = R.drawable.lei;
                break;
            case "xue":
                result = R.drawable.xue;
                break;


        }
        return result;
    }
}
